<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\common\errcode\ApiErrCode;
use app\common\errcode\ApiErrMsg;
use think\Loader;

class BrandController extends BaseController
{
    protected $brandModel;

    public function __construct()
    {
        parent::__construct();
        $this->brandModel = new \app\api\model\BrandModel();
    }

    /**
     * 新增品牌
     * @param   string  $brand          品牌名称
     * @param   array   $product        品牌产品
     * @param   array   $team_video     协同视频团队部门id
     * @param   array   $team_operate   协同运营团队部门id
     * @return json
     */
    public function create()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Brand');
                $check = $validate->scene('create')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                $exist = $this->brandModel->getOne($this->userid, $data['brand']);
                if($exist){
                    return $this->jsonJump(ApiErrCode::BRAND_EXIST,ApiErrMsg::BRAND_EXIST . ",创建者:{$exist}");
                }
                if($this->brandModel->add($this->userid, $data) === true){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::CREATE_BRAND_FAIL,ApiErrMsg::CREATE_BRAND_FAIL);
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 品牌列表
     * @param int       $page       页码
     * @param int       $pagesize   分页数量
     * @param string    $username   上传人
     * @param int       $brand      品牌
     * @return json     
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->brandModel->getList($this->userid,$this->departmentId,$data));         
            }        
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 编辑品牌
     * @param int       $brandid
     * @param string    $userid
     * @return json     
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Brand');
                $check = $validate->scene('edit')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->brandModel->edit($this->userid, $this->departmentId, $data)){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::EDIT_FAIL,ApiErrMsg::EDIT_FAIL);    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 删除品牌
     * @param int   $brandid
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Brand');
                $check = $validate->scene('delete')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->brandModel->del($this->userid, $data['brandid'])){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::DELETE_FAIL,ApiErrMsg::DELETE_FAIL);
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 备注品牌
     * @param string $remark
     * @param int   $brandid
     */
    public function remark()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Brand');
                $check = $validate->scene('remark')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->brandModel->remark($this->userid, $data['brandid'], !empty($data['remark']) ? trim($data['remark']) : '')){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::REMARK_FAIL,ApiErrMsg::REMARK_FAIL);
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 检查产品是否存在关联视频
     * @param int   $productid 产品id
     */
    public function check()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Brand');
                $check = $validate->scene('check')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->brandModel->findProduct($data['productid'])){
                    return $this->jsonJump(ApiErrCode::PRODUCT_EXIST_VIDEO,ApiErrMsg::PRODUCT_EXIST_VIDEO);    
                }
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }
}